package org.asg.dis.service;

import org.asg.dis.model.Shipment;
import org.asg.dis.model.ShipmentStatus;
import org.asg.dis.repository.ShipmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class ShipmentService {

    @Autowired
    private ShipmentRepository shipmentRepository;

    public Shipment saveShipment(Shipment shipment) {
        return shipmentRepository.save(shipment);
    }

    public Optional<Shipment> getShipmentById(String shipmentId) {
        return shipmentRepository.findById(UUID.fromString(shipmentId));
    }

    public List<Shipment> getAllShipments() {
        return shipmentRepository.findAll();
    }

    public void updateShipment(UUID shipmentId, Shipment updatedShipment) {
        Shipment existingShipment = shipmentRepository.findById(shipmentId)
                .orElseThrow(() -> new RuntimeException("Shipment not found"));
        // 更新字段
        existingShipment.setSenderId(updatedShipment.getSenderId());
        existingShipment.setSenderName(updatedShipment.getSenderName());
        existingShipment.setSenderPhoneNumber(updatedShipment.getSenderPhoneNumber());
        existingShipment.setSenderRegion(updatedShipment.getSenderRegion());
        existingShipment.setSenderDetailedAddress(updatedShipment.getSenderDetailedAddress());
        existingShipment.setReceiverName(updatedShipment.getReceiverName());
        existingShipment.setReceiverPhoneNumber(updatedShipment.getReceiverPhoneNumber());
        existingShipment.setReceiverRegion(updatedShipment.getReceiverRegion());
        existingShipment.setReceiverDetailedAddress(updatedShipment.getReceiverDetailedAddress());
        existingShipment.setExpressService(updatedShipment.getExpressService());
        existingShipment.setType(updatedShipment.getType());
        existingShipment.setWeight(updatedShipment.getWeight());
        existingShipment.setAmount(updatedShipment.getAmount());
        existingShipment.setNote(updatedShipment.getNote());
        existingShipment.setExpectedTime(updatedShipment.getExpectedTime());
        existingShipment.setPaymentMethod(updatedShipment.getPaymentMethod());
        existingShipment.setStatus(updatedShipment.getStatus());
        existingShipment.setLocation(updatedShipment.getLocation());
        shipmentRepository.save(existingShipment);
    }

    @Transactional
    public void updateShipment(UUID shipmentId, ShipmentStatus status) {
        Shipment target = shipmentRepository.findById(shipmentId)
                .orElseThrow(() -> new RuntimeException("Shipment not found with id: " + shipmentId));

        target.setStatus(status);

        shipmentRepository.save(target);
    }

    public void deleteShipment(UUID shipmentId) {
        shipmentRepository.deleteById(shipmentId);
    }

    public long getTotalShipmentCount() {
        return shipmentRepository.count();
    }

    public long getProblemShipmentCount() {
        return shipmentRepository.countByStatus(ShipmentStatus.异常);
    }
}
